﻿namespace Beetle
{
    using System;
    using System.Collections.Generic;

    public class ChannelDisposedQueue : Task
    {
        private Queue<ChannelDisposedEventArgs> _field_a = new Queue<ChannelDisposedEventArgs>(0x400);
        private static ChannelDisposedQueue _field_b = new ChannelDisposedQueue();

        private ChannelDisposedEventArgs a()
        {
            lock (this._field_a)
            {
                if (this._field_a.Count > 0)
                {
                    return this._field_a.Peek();
                }
                return null;
            }
        }

        public void Add(ChannelDisposedEventArgs channel)
        {
            lock (this._field_a)
            {
                this._field_a.Enqueue(channel);
            }
        }

        private void b()
        {
            lock (this._field_a)
            {
                if (this._field_a.Count > 0)
                {
                    this._field_a.Dequeue();
                }
            }
        }

        protected override void Execute()
        {
            for (ChannelDisposedEventArgs args = this.a(); args != null; args = this.a())
            {
                if ((Environment.TickCount - args._field_a) <= args.Delay)
                {
                    break;
                }
                args.Channel.b();
                args.Channel = null;
                this.b();
            }
        }

        public static ChannelDisposedQueue DisposedQueue
        {
            get
            {
                return _field_b;
            }
        }
    }
}

